<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:bil="http://java.sun.com/jsf/composite/bill"
      xmlns:phi="http://java.sun.com/jsf/composite/pharmacy"
      xmlns:pa="http://java.sun.com/jsf/composite/paymentMethod"
      xmlns:pat="http://java.sun.com/jsf/composite/patient">

    <h:body>

        <ui:composition template="/resources/template/template.xhtml">

            <ui:define name="content">



                <h:form id="bill" >
                    <p:commandButton id="nullButton2" value="No Action" action="#" style="display: none;"  onclick="onSubmitButton();"></p:commandButton>
                    <p:defaultCommand  target="btnAdd" />  

                    <p:panel rendered="#{!pharmacySaleController.billPreview}"  >
                        <h:panelGrid id="sale" columns="2" class="alignTop" >
                            <p:panel >
                                <f:facet name="header" >
                                    <h:outputLabel value="Pharmacy Retail Bill" />
                                </f:facet>
                                <h:panelGrid columns="1" >
                                    <h:panelGrid columns="6" >
                                        <h:panelGrid columns="1" >
                                            <p:outputLabel value="Medicines/Devices" ></p:outputLabel>
                                            <style>.ui-autocomplete-input {width:300px!important;}</style>
                                            <p:autoComplete accesskey="i"   forceSelection="true"  id="acStock" 
                                                            value="#{pharmacySaleController.stock}" 
                                                            converter="stockCon" 
                                                            completeMethod="#{pharmacySaleController.completeAvailableStocks}" 
                                                            var="i" itemLabel="#{i.itemBatch.item.name}" itemValue="#{i}" 
                                                            style="width: 50px!important;">
                                                <p:column headerText="Item">
                                                    <h:outputLabel value="#{i.itemBatch.item.name}" ></h:outputLabel>
                                                </p:column>
                                                <p:column headerText="Code">
                                                    <h:outputLabel value="#{i.itemBatch.item.code}" ></h:outputLabel>
                                                </p:column>
                                                <p:column headerText="Rate">
                                                    <h:outputLabel value="#{i.itemBatch.retailsaleRate}" >
                                                        <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                                                    </h:outputLabel>
                                                </p:column>
                                                <p:column headerText="Stocks">
                                                    <h:outputLabel value="#{i.stock}" >
                                                        <f:convertNumber pattern="#,###" ></f:convertNumber>
                                                    </h:outputLabel>
                                                </p:column>
                                                <p:column headerText="Expiry">
                                                    <h:outputLabel value="#{i.itemBatch.dateOfExpire}" >
                                                        <f:convertDateTime pattern="dd MMMM yyyy" ></f:convertDateTime>
                                                    </h:outputLabel>
                                                </p:column>
                                                <p:ajax event="focus" process="acStock :#{p:component('cmbPs')}"  ></p:ajax>
                                                <p:ajax event="itemSelect"   listener="#{pharmacySaleController.handleStockSelect}"  update="txtQty txtRate focusQty" ></p:ajax>
                                            </p:autoComplete>

                                        </h:panelGrid>
                                        <h:panelGrid columns="1" >
                                            <p:outputLabel value="Quentity" ></p:outputLabel>
                                            <p:inputText autocomplete="off"  accesskey="q"  id="txtQty" value="#{pharmacySaleController.qty}" style="width: 50px!important;"   >
                                                <p:ajax event="keyup"  listener="#{pharmacySaleController.handleQuentityEntryListner}" process="acStock txtQty :#{p:component('cmbPs')}" update="txtRate txtVal" ></p:ajax>
                                                <p:ajax event="blur"  listener="#{pharmacySaleController.handleQuentityEntryListner}" process="acStock txtQty :#{p:component('cmbPs')}" update="txtRate txtVal" ></p:ajax>
                                            </p:inputText>
                                        </h:panelGrid>
                                        <h:panelGrid columns="1" >
                                            <p:outputLabel value="Rate" ></p:outputLabel>
                                            <p:outputLabel id="txtRate" value="#{pharmacySaleController.billItem.netRate}" >
                                                <f:convertNumber pattern="#,##0.00" />
                                            </p:outputLabel>
                                        </h:panelGrid>
                                        <h:panelGrid columns="1" >
                                            <p:outputLabel value="Value" ></p:outputLabel>
                                            <p:outputLabel id="txtVal" value="#{pharmacySaleController.billItem.netValue}" >
                                                <f:convertNumber pattern="#,##0.00" />
                                            </p:outputLabel>
                                        </h:panelGrid>
                                        <p:commandButton  accesskey="a" id="btnAdd" value="Add" 
                                                         action="#{pharmacySaleController.addBillItem}" process="@this acStock txtQty :#{p:component('cmbPs')}" update=":#{p:component('pBillDetails')} :#{p:component('tblBillItem')} txtRate txtQty acStock focusItem tabI" onclick="onSubmitButton();" ></p:commandButton>

                                        <p:focus id="focusQty" for="txtQty" ></p:focus>
                                        <p:focus id="focusItem" for="acStock" ></p:focus>

                                    </h:panelGrid>


                                    <p:tabView id="tabI" dynamic="true" activeIndex="#{pharmacySaleController.activeIndex}" >

                                        <p:tab title="Availability" >
                                            <h:panelGrid columns="1" >
                                                <p:panel header="Not Available" >
                                                    <p:selectOneListbox id="lstSelectItems" style="width: 300px; height: 200px; max-width: 300px; max-height: 200px; min-width: 300px; min-height: 200px;" value="#{pharmacySaleController.selectedAlternative}"  >
                                                        <f:selectItems  value="#{pharmacySaleController.itemsWithoutStocks}" var="si" itemLabel="#{si.name}" itemValue="#{si}" ></f:selectItems>
                                                        <p:ajax event="change" process="lstSelectItems" update="lstReplaceableBatch" listener="#{pharmacySaleController.selectReplaceableStocks}" ></p:ajax>
                                                    </p:selectOneListbox>
                                                </p:panel>
                                                <p:panel header="Available" >
                                                    <p:selectOneListbox id="lstReplaceableBatch" style="width: 300px; height: 200px; max-width: 300px; max-height: 200px; min-width: 300px; min-height: 200px;"  >
                                                        <f:selectItems  value="#{pharmacySaleController.replaceableStocks}" var="rs" itemLabel="#{rs.itemBatch.item.name}" itemValue="#{rs}" ></f:selectItems>
                                                    </p:selectOneListbox>
                                                </p:panel>
                                            </h:panelGrid>
                                        </p:tab>
                                        <p:tab title="Bill Items" >
                                            <h:panelGrid columns="1">
                                                <p:panel header="Bill Items" id="pBis">
                                                    <p:dataTable id="tblBillItem" value="#{pharmacySaleController.preBill.billItems}"
                                                                 var="bi" rowIndexVar="s" editable="true" sortBy="#{bi.searialNo}" >

                                                        <p:ajax event="rowEdit" listener="#{pharmacySaleController.onEdit}" update="@this gros :#{p:component('pBillDetails')}" />  
                                                        <p:ajax event="rowEditCancel" listener="#{pharmacySaleController.onEdt}" update="@this gros :#{p:component('pBillDetails')}" /> 


                                                        <p:column headerText="Item">
                                                            <h:outputLabel value="#{bi.pharmaceuticalBillItem.itemBatch.item.name}" ></h:outputLabel>
                                                        </p:column>
                                                        <p:column headerText="Quentity">
                                                            <p:cellEditor>  
                                                                <f:facet name="output">  
                                                                    <h:outputText value="#{bi.qty}" >
                                                                        <f:convertNumber integerOnly="true" />
                                                                    </h:outputText>
                                                                </f:facet>  
                                                                <f:facet name="input">  
                                                                    <p:inputText autocomplete="off" id="ipTblQty" value="#{bi.qty}" style="width:96%"/>  
                                                                </f:facet>  
                                                            </p:cellEditor> 
                                                        </p:column>

                                                        <p:column style="width:6%">  
                                                            <p:rowEditor />  
                                                        </p:column>  


                                                        <p:column headerText="Rate">
                                                            <h:outputLabel id="rate" value="#{bi.rate}" >
                                                                <f:convertNumber pattern="#,##0.00" />
                                                            </h:outputLabel>
                                                        </p:column>
                                                        <p:column headerText="Value">
                                                            <h:outputLabel value="#{bi.grossValue}" id="gros">
                                                                <f:convertNumber pattern="#,##0.00" />
                                                            </h:outputLabel>
                                                        </p:column>
                                                        <p:column headerText="Expiry">
                                                            <h:outputLabel value="#{bi.pharmaceuticalBillItem.itemBatch.dateOfExpire}" >
                                                                <f:convertDateTime pattern="dd MM yyyy" ></f:convertDateTime>
                                                            </h:outputLabel>

                                                        </p:column>
                                                        <p:column headerText="Remove">
                                                            <p:commandButton value="Remove" action="#{pharmacySaleController.removeBillItem(bi)}" ajax="false" onclick="onSubmitButton();" >

                                                            </p:commandButton>
                                                        </p:column>

                                                    </p:dataTable>
                                                </p:panel>
                                            </h:panelGrid>
                                        </p:tab>
                                    </p:tabView>


                                </h:panelGrid>

                            </p:panel>




                            <p:panel>



                                <f:facet name="header" >
                                    <h:panelGrid columns="20"  >

                                        <h:outputLabel value="&nbsp;&nbsp;&nbsp;&nbsp;" style="min-width: 50px;" />
                                        <p:selectOneMenu   id="pay" value="#{pharmacySaleController.paymentMethod}">    
                                            <f:selectItems value="#{enumController.paymentMethods}"/>
                                            <p:ajax process="@this cmbPs " 
                                                    update="pBillDetails creditCard cheque slip" 
                                                    event="change"  
                                                    listener="#{pharmacySaleController.changeListener()}"/>
                                        </p:selectOneMenu>

                                        <p:selectOneMenu   id="cmbPs" value="#{pharmacySaleController.paymentScheme}">    
                                            <f:selectItem itemLabel="Discount Scheme"/>
                                            <f:selectItems value="#{paymentSchemeController.paymentSchemesForPharmacy}" var="i" itemLabel="#{i.name}" itemValue="#{i}"/>
                                            <p:ajax process="@this pay " 
                                                    update="pBillDetails creditCard cheque slip" 
                                                    event="change"  
                                                    listener="#{pharmacySaleController.changeListener()}"/>
                                        </p:selectOneMenu>

                                        <h:panelGroup id="creditCard" style="display: #{pharmacySaleController.paymentMethod ne 'Card' ? 'none' : 'block'} ; ">
                                            <pa:creditCard paymentMethodData="#{pharmacySaleController.paymentMethodData}"/>
                                        </h:panelGroup>

                                        <h:panelGroup id="cheque" style="display: #{pharmacySaleController.paymentMethod ne 'Cheque' ? 'none' : 'block'} ; " >
                                            <pa:cheque paymentMethodData="#{pharmacySaleController.paymentMethodData}"/>
                                        </h:panelGroup>

                                        <h:panelGroup id="slip" style="display: #{pharmacySaleController.paymentMethod ne 'Slip' ? 'none' : 'block'} ;">
                                            <pa:slip paymentMethodData="#{pharmacySaleController.paymentMethodData}"/>
                                        </h:panelGroup>

                                        <p:commandButton ajax= "false" accesskey="s" value="Settle" 
                                                        action="#{pharmacySaleController.settleBillWithPay}"                                                          
                                                        actionListener="#{pharmacySaleController.calculateAllRates}" onclick="onSubmitButton();">
                                            <f:facet name="title" >
                                                <h:outputLabel value="S" style="text-decoration: underline;" ></h:outputLabel>
                                                <h:outputLabel value="ettle"  ></h:outputLabel>
                                            </f:facet>
                                        </p:commandButton>
                                        <p:commandButton accesskey="n" value="New Pharmacy Bill" ajax="false" action="pharmacy_bill_retail_sale"
                                                        actionListener="#{pharmacySaleController.resetAll()}" onclick="onSubmitButton();"  ></p:commandButton>


                                    </h:panelGrid>


                                </f:facet>





                                <p:panel header="Bill Details" id="pBillDetails"  >
                                    <p:panelGrid columns="2" columnClasses="numberCol, textCol"  >
                                        <h:outputLabel value="Total" ></h:outputLabel>
                                        <h:outputLabel id="total" value="#{pharmacySaleController.preBill.total}" >
                                            <f:convertNumber pattern="#,##0.00" />
                                        </h:outputLabel>
                                        <h:outputLabel value="Discount" ></h:outputLabel>
                                        <h:outputLabel id="dis" value="#{pharmacySaleController.preBill.discount}" >
                                            <f:convertNumber pattern="#,##0.00" />
                                            <p:ajax process="total dis netTotal paid" update="balance netTotal" event="blur"/>
                                        </h:outputLabel>
                                        <h:outputLabel value="Service Charge" ></h:outputLabel>
                                        <p:inputText autocomplete="off" id="service" value="#{pharmacySaleController.preBill.serviceCharge}" >
                                            <p:ajax process="total dis netTotal service paid" update="balance netTotal" event="blur" listener="#{pharmacySaleController.calTotal}" />
                                        </p:inputText>
                                        <h:outputLabel value="Net Total" ></h:outputLabel>
                                        <h:outputLabel id="netTotal" value="#{pharmacySaleController.preBill.netTotal}" >
                                            <f:convertNumber pattern="#,##0.00" />
                                        </h:outputLabel>
                                        <h:outputLabel  value="Tendered" ></h:outputLabel>
                                        <p:inputText autocomplete="off" accesskey="t" id="paid" value="#{pharmacySaleController.cashPaid}" >
                                            <p:ajax process="total dis netTotal paid" update="balance netTotal" event="blur"/>
                                        </p:inputText>
                                        <h:outputLabel  value="Balance " ></h:outputLabel>
                                        <h:outputLabel id="balance" value="#{pharmacySaleController.balance}" >
                                            <f:convertNumber pattern="#,##0.00" />
                                        </h:outputLabel>
                                    </p:panelGrid>

                                </p:panel>

                                <p:panel header="Patient Detail">                                        
                                      
                                    
                                    <p:tabView id="tvPt" style="min-height: 250px;"  activeIndex="#{pharmacySaleController.patientSearchTab}" >
                                        <p:ajax event="tabChange"  process="@this"
                                                update=":#{p:component('cmbPs')} :#{p:component('pBillDetails')}"
                                                listener="#{pharmacySaleController.onTabChange}" />

                                        <p:tab id="tabNewPt" title="New Patient"  >
                                            <bil:newPatient controller="#{pharmacySaleController}"/>
                                        </p:tab>

                                        <p:tab id="tabSearchPt" title="Search Patient" >


                                            <p:autoComplete
                                                widgetVar="aPt" 
                                                id="acPt" forceSelection="true" 
                                                value="#{pharmacySaleController.searchedPatient}" 
                                                completeMethod="#{patientController.completePatientByNameOrCode}" 
                                                var="apt" itemLabel="#{apt.person.name}" 
                                                itemValue="#{apt}" size="30"  
                                                style="width: 400px;"
                                                disabled="#{pharmacySaleController.fromOpdEncounter}">
                                                <p:ajax event="itemSelect" process="@this" update=":#{p:component('cmbPs')} :#{p:component('pBillDetails')} panSearch"
                                                        listener="#{pharmacySaleController.searchPatientListener}"/>
                                            </p:autoComplete>  
                                            <h:panelGroup id="panSearch">
                                                <pat:searchPatientDetail patient="#{pharmacySaleController.searchedPatient}"  />
                                            </h:panelGroup>
                                            <div style="padding: 2px; margin: 2px; border: 1px solid;">
                                                <p:outputLabel value="#{pharmacySaleController.opdEncounterComments}" ></p:outputLabel>
                                            </div>
                                        </p:tab>

                                        <p:tab id="tabCredit" title="Credit"  >
                                            <p:panelGrid columns="3" >
                                                <p:outputLabel value="Credit Company" ></p:outputLabel>
                                                <p:autoComplete  style="width: 400px;" 

                                                                 forceSelection="true" 
                                                                 value="#{pharmacySaleController.toInstitution}" 
                                                                 id="scCreditCompany"
                                                                 completeMethod="#{institutionController.completeCreditCompany}" 
                                                                 var="myi" itemLabel="#{myi.name}" 
                                                                 itemValue="#{myi}"
                                                                 converter="institutionConverter" 
                                                                 disabled="#{pharmacySaleController.paymentMethod ne 'Credit' }">

                                                    <p:ajax process="scCreditCompany" update=":#{p:component('pBillDetails')}" 
                                                            event="itemSelect"  listener="#{pharmacySaleController.paymentSchemeChanged}" />

                                                </p:autoComplete>
                                                <p:watermark for="scCreditCompany" value="Type at least 3 charactors of Company" ></p:watermark>

                                                <p:outputLabel value="Staff Member" ></p:outputLabel>
                                                <p:autoComplete  style="width: 400px;" 
                                                                 forceSelection="true" 
                                                                 value="#{pharmacySaleController.toStaff}" 
                                                                 id="scStaff"
                                                                 completeMethod="#{staffController.completeStaff}" 
                                                                 var="mys" 
                                                                 itemLabel="#{mys.person.nameWithTitle}" 
                                                                 itemValue="#{mys}"
                                                                 converter="stfcon" 
                                                                 disabled="#{pharmacySaleController.paymentMethod ne 'Credit' }">
                                                    <p:column headerText="Name" >
                                                        <h:outputLabel value="#{mys.person.nameWithTitle}" ></h:outputLabel>
                                                    </p:column>
                                                    <p:column headerText="Annual Entitlement">
                                                        <h:outputLabel value="#{mys.annualWelfareQualified}" >
                                                            <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                                                        </h:outputLabel>
                                                    </p:column>
                                                    <p:column  headerText="Annual Utilized Value">
                                                        <h:outputLabel value="#{mys.annualWelfareUtilized}" >
                                                            <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                                                        </h:outputLabel>
                                                    </p:column>

                                                    <p:ajax process="scStaff" update=":#{p:component('pBillDetails')}" 
                                                            event="itemSelect"  listener="#{pharmacySaleController.paymentSchemeChanged}" />

                                                </p:autoComplete>
                                                <p:watermark for="scStaff" value="Type at least 3 charactors of Staff member." ></p:watermark>
                                            </p:panelGrid>
                                        </p:tab>



                                    </p:tabView>
                                
                                
                                </p:panel>



                            </p:panel>

                        </h:panelGrid>
                    </p:panel>


                </h:form>

                <h:form>


                    <p:panel  rendered="#{pharmacySaleController.billPreview}" >




                        <p:commandButton id="nullButton3" value="No Action" action="#" style="display: none;" onclick="onSubmitButton();" ></p:commandButton>
                        <p:defaultCommand  target="btnPrint" />  

                        <div class="nonPrintBlock" >
                            <p:commandButton accesskey="p" id="btnPrint" value="Print" ajax="false" action="#" onclick="onSubmitButton();" >
                                <p:printer target="gpBillPreview" ></p:printer>
                            </p:commandButton>
                            <p:commandButton accesskey="n" value="Back to Queue" ajax="false" 
                                            action="/clinical/clinical_queue.xhtml" actionListener="#{pharmacySaleController.resetAll()}" onclick="onSubmitButton();" ></p:commandButton>

                        </div>
                        <div >
                            <h:panelGroup   id="gpBillPreview"   > 
                                <phi:clinical_pharmacy_sale_bill bill="#{pharmacySaleController.printBill}"></phi:clinical_pharmacy_sale_bill>
                            </h:panelGroup>
                        </div>



                    </p:panel>



                </h:form>

            </ui:define>

        </ui:composition>

    </h:body>
</html>
